package cn.com.xinjinfu.pdoulcProduct.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.util.Map;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.ActTransactionDetailed;
import cn.com.xinjinfu.pdoulcProduct.activity.AllIncomActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.FixedProduct.ActivitMoneyAct;
import cn.com.xinjinfu.pdoulcProduct.activity.FixedProduct.LandlordIncomeAct;
import cn.com.xinjinfu.pdoulcProduct.activity.recharge.ActRechargeBinding;
import cn.com.xinjinfu.pdoulcProduct.activity.recharge.ActWithdrawDeposit;
import cn.com.xinjinfu.pdoulcProduct.activity.user.ActCertification;
import cn.com.xinjinfu.pdoulcProduct.activity.user.AllMoneyActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.user.BankBangdingAct;
import cn.com.xinjinfu.pdoulcProduct.activity.user.MyInvestListAct;
import cn.com.xinjinfu.pdoulcProduct.activity.user.MyVoucherListAct;
import cn.com.xinjinfu.pdoulcProduct.activity.user.ReturnedMoneyAct;
import cn.com.xinjinfu.pdoulcProduct.activity.user.SetupInfoAct;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.HomeBean;
import cn.com.xinjinfu.pdoulcProduct.entity.MyPdouBean;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.JSONManager;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.utils.SharedPreferencesUtils;
import cn.com.xinjinfu.pdoulcProduct.utils.TextViewUtil;

/**
 * 我的P兜fragment1
 */
public class MyPdouFragment1 extends BaseFragment implements OnClickListener {
    private Activity mActivity;
    private User user;
    private MyPdouBean myPdou;
    private RequestQueue requen;
    private LinearLayout top_left_ll,ll_zongzichan,ll_leijishouyi,top_right_ll;
    private TextView tv_title,tv_balance,tv_freeze_money,tv_act_money,tv_invest_money,tv_landlord_money,tv_zongzichan
            ,tv_all_income,tv_wait_income,tv_voucher_num;
    private Button bt_tixian,bt_chongzhi;
    private RelativeLayout rl_trade_record,rl_activity_money,rl_my_invest,rl_repayment_plan,rl_voucher,
    rl_balance_bg,rl_landlord_income;
    private double balance;
    private ImageView img_num_hine;
    private CheckBox num_hine;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mActivity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mypdou, null);
        initViews(view);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        requen = Volley.newRequestQueue(mActivity);
        loginRequest();
        initOnChangeListener();
    }

    private void initOnChangeListener() {
        num_hine.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    img_num_hine.setBackgroundResource(R.drawable.img_yan1);
                    SharedPreferencesUtils.saveBoolean(mActivity, "is_hind_money", false);
                    if (myPdou != null) {
                        tv_balance.setText(TextViewUtil.getSumLength(balance));
                        tv_all_income.setText(TextViewUtil.getSumLength(myPdou.getTotalIncome()));
                        tv_zongzichan.setText(TextViewUtil.getSumLength(myPdou.getTotalAssert()));
                        tv_wait_income.setText(TextViewUtil.getSumLength(myPdou.getBeRecievedIncome()));

                        tv_invest_money.setText(TextViewUtil.getSumLength(myPdou.getInvestAmount()));
                        tv_landlord_money.setText(TextViewUtil.getSumLength(myPdou.getBrokerIncome()));
                    }
                } else {
                    SharedPreferencesUtils.saveBoolean(mActivity, "is_hind_money", true);
                    img_num_hine.setBackgroundResource(R.drawable.img_yan2);
                    tv_all_income.setText("***");
                    tv_zongzichan.setText("***");
                    tv_wait_income.setText("***");
                    tv_balance.setText("***");
                    tv_invest_money.setText("***");
                    tv_landlord_money.setText("***");
                }
            }
        });
    }
    /**
     * 网络请求
     */
    public void loginRequest() {
        user = AccountManager.getInstance().getUser();
        if (user != null) {
            final Map<String, String> parameters = DataHandler.getParameters("1009");
            parameters.put("id", user.getCurrUser().getSign());
            JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters), null, succeedRespon, error);
            requen.add(request);
        }
    }
    private Response.ErrorListener error = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            DataHandler.errorHandler(mActivity, error);
            if (SharedPreferencesUtils.getUserInfo(mActivity, "myPdou") != ""){
                myPdou=JSON.parseObject(SharedPreferencesUtils.getUserInfo(mActivity, "myPdou"), MyPdouBean.class);
                upData();
            }
        }
    };
    private Response.Listener<JSONObject> succeedRespon = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
            L.i(jobj.toString());
            SharedPreferencesUtils.saveUserInfo(mActivity, "myPdou", jobj.toString());
            if (JSONManager.getError(jobj) == -1){
                myPdou = JSON.parseObject(jobj.toString(), MyPdouBean.class);
                upData();
            }

        }
    };

    //初始化控件
    private void initViews(View view) {
        //初始化头布局
        top_left_ll = (LinearLayout)view.findViewById(R.id.top_left_ll);
        top_left_ll.setVisibility(View.VISIBLE);
        ImageView top_left_icon0 = (ImageView)view.findViewById(R.id.top_left_icon0);
        top_left_icon0.setImageResource(R.drawable.img_zh);
        tv_title = (TextView) view.findViewById(R.id.top_title_tv);
        tv_title.setText("我的P兜");
        top_right_ll = (LinearLayout) view.findViewById(R.id.top_right_ll);
        top_right_ll.setVisibility(View.VISIBLE);
        TextView top_right_tv = (TextView) view.findViewById(R.id.top_right_tv);
        top_right_tv.setText("交易记录");
        top_left_ll.setOnClickListener(this);
        top_right_ll.setOnClickListener(this);

        ll_zongzichan = (LinearLayout) view.findViewById(R.id.ll_zongzichan);
        ll_leijishouyi = (LinearLayout) view.findViewById(R.id.ll_leijishouyi);
        tv_zongzichan = (TextView) view.findViewById(R.id.tv_zongzichan);
        tv_balance = (TextView) view.findViewById(R.id.tv_balance);
        tv_all_income = (TextView) view.findViewById(R.id.tv_all_income);

        ll_zongzichan.setOnClickListener(this);
        ll_leijishouyi.setOnClickListener(this);
        num_hine = (CheckBox) view.findViewById(R.id.num_hine);
        img_num_hine = (ImageView) view.findViewById(R.id.img_num_hine);

        bt_tixian = (Button) view.findViewById(R.id.bt_tixian);
        bt_chongzhi = (Button) view.findViewById(R.id.bt_chongzhi);

        rl_my_invest = (RelativeLayout) view.findViewById(R.id.rl_my_invest);
        rl_repayment_plan = (RelativeLayout) view.findViewById(R.id.rl_repayment_plan);
        rl_voucher = (RelativeLayout) view.findViewById(R.id.rl_voucher);
        rl_landlord_income = (RelativeLayout) view.findViewById(R.id.rl_landlord_income);
        tv_wait_income = (TextView) view.findViewById(R.id.tv_wait_income);
        tv_invest_money = (TextView) view.findViewById(R.id.tv_invest_money);
        tv_landlord_money = (TextView) view.findViewById(R.id.tv_landlord_money);
        tv_voucher_num = (TextView) view.findViewById(R.id.tv_voucher_num);

        bt_tixian.setOnClickListener(this);
        bt_chongzhi.setOnClickListener(this);
        rl_my_invest.setOnClickListener(this);
        rl_repayment_plan.setOnClickListener(this);
        rl_voucher.setOnClickListener(this);
        rl_landlord_income.setOnClickListener(this);


    }

    public void upData(){
        balance = myPdou.getBalance();
        if (!SharedPreferencesUtils.getBoolean(mActivity,"is_hind_money", true)){
            img_num_hine.setBackgroundResource(R.drawable.img_yan1);
            tv_balance.setText(TextViewUtil.getSumLength(balance));
            tv_all_income.setText(TextViewUtil.getSumLength(myPdou.getTotalIncome()));
            tv_zongzichan.setText(TextViewUtil.getSumLength(myPdou.getTotalAssert()));
            tv_wait_income.setText(TextViewUtil.getSumLength(myPdou.getBeRecievedIncome()));

            tv_invest_money.setText(TextViewUtil.getSumLength(myPdou.getInvestAmount()));
            tv_landlord_money.setText(TextViewUtil.getSumLength(myPdou.getBrokerIncome()));
        }else {
            img_num_hine.setBackgroundResource(R.drawable.img_yan2);
            tv_all_income.setText("***");
            tv_zongzichan.setText("***");
            tv_wait_income.setText("***");
            tv_balance.setText("***");
            tv_invest_money.setText("***");
            tv_landlord_money.setText("***");
        }
        tv_voucher_num.setText(myPdou.getTotalRows()+"");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_left_ll:
                startActivity(new Intent(mActivity,SetupInfoAct.class));
                break;
            case R.id.top_right_ll:
                startActivity(new Intent(mActivity,ActTransactionDetailed.class));
                break;
            case R.id.rl_my_invest:
                Intent intent0 = new Intent(mActivity, MyInvestListAct.class);
                Bundle mBundle0 = new Bundle();
                mBundle0.putInt("xItem", 0);
                intent0.putExtras(mBundle0);
                startActivity(intent0);
                break;
            case R.id.rl_repayment_plan:
                startActivity(new Intent(mActivity,ReturnedMoneyAct.class));
                break;
            case R.id.rl_voucher:
                startActivity(new Intent(mActivity,MyVoucherListAct.class));
                break;
            case R.id.rl_landlord_income:
                startActivity(new Intent(mActivity,LandlordIncomeAct.class));
                break;
            case R.id.bt_chongzhi:
                Intent intent = new Intent(mActivity, ActRechargeBinding.class);
                Bundle mBundle = new Bundle();
                mBundle.putDouble("balance", balance);
                intent.putExtras(mBundle);
                startActivity(intent);
                break;
            case R.id.bt_tixian:
                /** *  还要在此判断是否实名认证  ***/
                if (user.getCurrUser().getIdNumber() == null) {
                    Intent intent2 = new Intent();
                    intent2.putExtra("cer_style_falg", 1);
                    intent2.setClass(mActivity, ActCertification.class);
                    startActivity(intent2);
                    ToastManager.showShort(mActivity, "请先实名认证！");
                }else {
                    if (user.getBank() == -1) {
                        Intent intent1 = new Intent(mActivity, ActWithdrawDeposit.class);
                        Bundle mBundle1 = new Bundle();
                        mBundle1.putDouble("balance", balance);
                        intent1.putExtras(mBundle1);
                        startActivity(intent1);
                    } else {
                        startActivity(new Intent(mActivity, BankBangdingAct.class));
                        ToastManager.showShort(mActivity, "请先绑定银行卡！");
                    }
                }
                break;

            case R.id.ll_zongzichan:
                startActivity(new Intent(mActivity, AllMoneyActivity.class));
                break;
            case R.id.ll_leijishouyi:
                startActivity(new Intent(mActivity, AllIncomActivity.class));
                break;

            default:
                break;
        }
    }


    @Override
    public String getFragmentName() {
        return null;
    }

    @Override
    protected void lazyLoad() {

    }

}
